package com.dlc.felear.xiaoensale.activity;

import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.adapter.SfPositionItemAdapter;
import com.dlc.felear.xiaoensale.adapter.SfSettingItemAdapter;
import com.dlc.felear.xiaoensale.constant.TempData;
import com.dlc.felear.xiaoensale.customView.LoadingDialog;
import com.dlc.felear.xiaoensale.customView.ShowTispMsgDialog;
import com.dlc.felear.xiaoensale.customView.SingleInputDialog;
import com.dlc.felear.xiaoensale.deviceControlSf.HcSfControl;
import com.dlc.felear.xiaoensale.deviceUtil.Serialhelper;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.entity.SfStInfo;
import com.dlc.felear.xiaoensale.helper.Session;
import com.itdlc.android.library.base.BaseActivity;

import org.simple.eventbus.EventBus;

import butterknife.BindView;

/**
 * 禾川伺服电机设置页面
 */
public class SfMotorSettingActivity extends BaseActivity implements SingleInputDialog.SingleDialogCallback{
    private static final String TAG = "SfMotorSettingActivity";
    public static final String ADDR = "addr";
    @BindView(R.id.tv_tisp)
    TextView tvTis;
    @BindView(R.id.tv_sf_tips)
    TextView tvSfTis;
    @BindView(R.id.sf_gv)
    GridView sfGv1;
    @BindView(R.id.sf_pgv)
    GridView sfGv2;
    private int addr=1;
    private TopbarHelper topbarHelper;
    private LoadingDialog loadingDialog;
    private SfSettingItemAdapter sfSettingItemAdapter;
    private HcSfControl sfControl;
    private SingleInputDialog singleInputDialog;

    private ShowTispMsgDialog showTispMsgDialog;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_step_m1_sf;
    }

    @Override
    protected void initialView() {
        EventBus.getDefault().register(this);
        loadingDialog=new LoadingDialog(this,"正在执行..");
        topbarHelper = new TopbarHelper(this);
        topbarHelper.setNavActivity(MotorPagerActivity.class);
        topbarHelper.setTitle("伺服电机设置");
        topbarHelper.setcon(R.drawable.arrow_left);
        sfSettingItemAdapter=new SfSettingItemAdapter(this);
        sfGv1.setAdapter(sfSettingItemAdapter);
        sfSettingItemAdapter.setItems(TempData.getSfItem1(1));
        sfControl=HcSfControl.getInstance(Serialhelper.getInstance().getDjSerialNo(),
                Session.getSession().getString("D_GT_TYPE","M400"));
        singleInputDialog=new SingleInputDialog(this,this);
        singleInputDialog.setEtInputType(InputType.TYPE_CLASS_TEXT);
        sfGv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                handlesendData(sfSettingItemAdapter.getItem(i));
            }
        });
        addr=getIntent().getIntExtra(ADDR,0);
        if(addr==2){
            tvSfTis.setText("在该页面转动电机时，确保翻料盒处于垂直状态（格子门在垂直方向上方），锅体不能 360度旋转，转动时不能把头和手伸进箱体里面！！！");
            tvTis.setText("锅体伺服电机参数设置");
        }else {
            tvSfTis.setText("在该页面转动电机时，确保锅体处于垂直状态和所有格子门已经关闭，翻料盒不能 360度旋转，转动时不能把头和手伸进箱体里面！！！");
            tvTis.setText("翻料盒伺服电机参数设置");
        }
        initposition();
        Log.e(TAG,"addr="+addr);
    }


    private void initposition(){
        sfGv2.setAdapter(new SfPositionItemAdapter(this,addr));
    }

    private void handlesendData(SfStInfo sfStInfo){
        loadingDialog.show(500);
        switch (sfStInfo.getName()){
            case "报警信息":
                showLongToast(sfControl.getSfErrMsg());
                break;
            case "清除报警":
                sfControl.resetSf(addr);
                break;
            case "电机正转":
                sfControl.singgleStep(addr,0);
                break;
            case "电机反转":
                sfControl.singgleStep(addr,1);
                break;
            case "电机停止":
                sfControl.excutSfRun(addr,false);
                break;
            case "设置原点":
                showSingleDialog(1,"设置原点","是否重新设置原点");
                break;
            case "重置编码器":
                showSingleDialog(2,"重置编码器","是否重置编码器");
                break;
            case "运行指定点":
                showSingleDialog(0,"指定运行","输入运行段号（1-8）");
                break;

        }
    }


    private void showSingleDialog(int id,String tips,String tipsValue){
        singleInputDialog.setTips(tips);
        singleInputDialog.setHintMsg(tipsValue);
        singleInputDialog.showDialog(id);
    }


    @Override
    public void SingleDialogInputResult(String value, int id) {
        Log.e(TAG,"value="+value);
        if(id==0){
            sfControl.runDesignatedSpot(addr,Integer.parseInt(value));
           return;
        }
        if(!TextUtils.equals(value,"234561")){
            showLongToast("口令错误");
            return;
        }
        try {
            switch (id){
                case 0:
                    sfControl.runDesignatedSpot(addr,Integer.parseInt(value));
                    break;
                case 1:
                    sfControl.resetYd(addr);
                    break;
                case 2:
                    sfControl.resetBmq(addr);
                    break;
            }
        }catch (Exception e){
            Log.e(TAG,e.getMessage());
            showLongToast("请输入有效的参数");
        }
    }

}